import { Injectable } from '@angular/core';
import { AppConfig } from "../config";
import { Http, Headers } from "@angular/http";
import { Observable } from "rxjs/Observable";
import { MatrixStateGroup } from "../entity/matrix-state-group.model";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class MatrixStateGroupService {
  private readonly param = "matrixStateGroups";
  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService) { }

  /**
   * 获得矩阵状态分组
   * 
   * @returns 
   * @memberof MatrixStateGroupService
   */
  getMatrixStateGroups(): Promise<MatrixStateGroup[]> {
    return this.http.get(this.url)
      .toPromise()
      .then(response => response.json().data as MatrixStateGroup[])
      .catch(this.handleError);
  }

  /**
   * 根据tagId获得矩阵状态分组
   * 
   * @returns 
   * @memberof MatrixStateGroupService
   */
  getMatrixStateGroupsByTagId(tagId: number): Promise<MatrixStateGroup[]> {
    //const apiUrl = `http://192.168.15.150:8585/webapi/v1/matrixStateGroups?items=${keyIds}`;
    return this.http.get(`${this.url}/tagId/${tagId}`)
      .toPromise()
      .then(response => response.json().data as MatrixStateGroup[])
      .catch(this.handleError);
  }

  create(matrixStateGroup: MatrixStateGroup): Promise<MatrixStateGroup> {
    //this.url
    return this.http.post(this.url, JSON.stringify(matrixStateGroup))
      .toPromise()
      .then(res => {
        try {
          let result = res.json();
          switch (result.code) {
            case "200":
              return result.data as MatrixStateGroup;
            case "-1":
              throw new Error(result.message);
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }
    
  update(matrixStateGroup: MatrixStateGroup): Promise<MatrixStateGroup> {
    const url = `${this.url}`;
    return this.http.put(url, JSON.stringify(matrixStateGroup))
      .toPromise()
      .then(() => matrixStateGroup)
      .catch(this.handleError);
  }

  //根据Name判断是否已经存在
  hasStateGroupName(stateGroupName: string, stateGroupId: number,tagId: number): Observable<boolean> {
    //http://192.168.15.150:8585/webapi/v1/matrixStateGroups/
    const url = `${this.url}/stateGroupName/${stateGroupName}/stateGroupId/${stateGroupId}/tagId/${tagId}`;
    return this.http.get(url).map(response => {
      let code = response.json().code;
      switch (code) {
        case "3001":
          return true;
        case "3002":
          return false;
      }
    }).catch(this.handleError);
  }

  //删除
  delete(id: number): Promise<void> {
    const url = `${this.url}/${id}`;
    return this.http.delete(url)
      .toPromise()
      .then(() => null)
      .catch(this.handleError);
  }

  //批量删除
  batchDelete(keyIds: number[]): Promise<boolean> {
    const apiUrl = `${this.url}?items=${keyIds}`;
    return this.http.delete(apiUrl)
      .toPromise()
      .then(response => {
        try {
          let result = response.json();
          switch (result.code) {
            case "200":
              return true;
            case "-1":
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }



}



// WEBPACK FOOTER //
// ./src/app/service/matrix-state-group.service.ts